package com.guanzi.big_data_mq_process.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guanzi.big_data_mq_process.dao.TransactionMessageDao;
import com.guanzi.big_data_mq_process.model.entity.TransactionMessage;
import com.guanzi.big_data_mq_process.model.enums.LocalTransactionStateEnum;
import com.guanzi.big_data_mq_process.model.enums.MessageStatusEnum;
import com.guanzi.big_data_mq_process.service.ITransactionMessageService;
import org.springframework.stereotype.Service;

/**
 * (TransactionMessage) 服务实现类
 *
 * @author xuguang
 * @since 2025-05-07 14:41:50
 */
@Service
public class TransactionMessageServiceImpl extends ServiceImpl<TransactionMessageDao, TransactionMessage> implements ITransactionMessageService {

    @Override
    public void updateTxState(String messageId, LocalTransactionStateEnum localTransactionStateEnum) {
        new UpdateChainWrapper<>(this.baseMapper)
                .set("tx_state", localTransactionStateEnum)
                .eq("message_id", messageId)
                .update();
    }

    @Override
    public void updateMsgStatus(String messageId, MessageStatusEnum messageStatusEnum) {
        new UpdateChainWrapper<>(this.baseMapper)
                .set("status", messageStatusEnum.getCode())
                .eq("message_id", messageId)
                .update();
    }

    @Override
    public LocalTransactionStateEnum selectTxState(String messageId) {
        TransactionMessage localMsg = this.baseMapper.selectOne(Wrappers.<TransactionMessage>query().select("tx_state").eq("message_id", messageId));
        return localMsg.getTxState();
    }
}
